package com.lse_from.config.async

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.task.TaskExecutor
import org.springframework.scheduling.annotation.EnableAsync
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import java.util.concurrent.Executor
import java.util.concurrent.ThreadPoolExecutor

/**
 * Async注解
 * 异步多线程配置
 *
 * @author 祁雪
 */
@Configuration
@EnableAsync
open class AsyncConfig {

    @Bean
    open fun executor(): Executor {

        val executor = ThreadPoolTaskExecutor()

        // 核心线程
        // 核心线程
        executor.corePoolSize = 2
        // 最大线程
        // 最大线程
        executor.maxPoolSize = 5
        // 队列大小
        // 队列大小
        executor.setQueueCapacity(100)
        // 线程最大空闲时间
        // 线程最大空闲时间
        executor.keepAliveSeconds = 500
        // 新线程名称前缀
        // 新线程名称前缀
        executor.setThreadNamePrefix("executor-")
        executor.setRejectedExecutionHandler(ThreadPoolExecutor.CallerRunsPolicy())
        return executor

    }

}